using Microsoft.AspNetCore.Mvc;
using System.Text;

namespace Ex01.Controllers {
  public class MathController : Controller {
    public IActionResult Index() {
      return View();
    }

    public ulong GCD(ulong x, ulong y) {
      while (y != 0) {
        ulong oldX = x;
        x = y;
        y = oldX % y;
      }
      return x;
    }

    public ulong LCM(ulong x, ulong y) {
      return (x * y) / GCD(x, y);
    }

    public string Greeting() {
      return "Welcome to MyMath controller";
    }

    [NonAction]
    public string AnotherGreeting() {
      return "This is non-action!";
    }

    public string Fibonacci(uint? n) {
      n = n ?? 2;
      if (n > 1) {
        StringBuilder sb = new StringBuilder();
        uint x = 0, y = 1;
        for (int i = 0; i < n; i++) {
          sb.Append($" {x}");
          y += x;
          x = y - x;
        }
        return sb.ToString();
      } else {
        return "n must be at least 2";
      }
    }
    /*
     Try call the following:

       ./Math/Fibonacci?n=10
       ./Math/Fibonacci
       ./Math/Fibonacci?n=1

     */
  }
}
